import router from "@/router";
export default {
    state: {
        tagList: [{
            title: "home",
            path: "/index/home",
            name: 'homeName'
        }]
    },
    getters: {},
    mutations: {
        // 点击标签
        switTag(state, item) {
            router.push(item.path)  //点击标签跳转相对路由
        },
        // 移除标签
        handleClose(state, ind) {
            if (state.tagList[ind].path == router.currentRoute._value.fullPath) {  //路由等于当前路由
                let path = (state.tagList[ind + 1] || state.tagList[ind - 1]).path //删除最末尾选中后面的ind-1
                router.push(path)
            }
            state.tagList.splice(ind, 1)
        },
        // 右击刷新
        refresh(state, ind) {
            router.replace({
                name:'refreshview',
                query: {
                    path: state.tagList[ind].path
                }
            })
        },
        //关闭右侧标签
        colseRight(state, ind) {
            let num = state.tagList.findIndex(i => i.path == router.currentRoute._value.fullPath) //获取当前状态下标
            state.tagList.splice(ind + 1)
            if (num > ind) {  //下标小于当前页面跳转
                router.push(state.tagList[ind].path)
            }
        },
        // 全部关闭
        closeAll(state) {
            state.tagList.splice(1)
            router.push("/index/home")
        },
        // 添加标签
        option(state, route) {
            if (route.name == 'Index') return //到index路由不添加标签
            let arr = state.tagList.findIndex(i => i.path == route.path)
            let ind = state.tagList.findIndex(item => item.name == 'CommListName')  //查找商品列表下标
            if ((route.name == 'createName' || route.name == 'editName') && ind != -1 && arr == -1) {  //添加商品标签
                state.tagList.splice(ind + 1, 0, { ...route.meta, path: route.path, name: route.name })  //商品末尾添加标签
            } else if (- 1 == arr) {  //添加去重操作 
                state.tagList.push({
                    ...route.meta,
                    path: route.path,
                    name: route.name
                })
            } else {
                state.tagList[arr].path = route.path
            }
        },
        // 关闭按钮
        close(state) {
            let ind = state.tagList.findIndex(item => item.path == router.currentRoute._value.fullPath)
            state.tagList.splice(ind, 1)
            router.push({ name: 'CommListName' })
        }
    },
    actions: {
        switTag(context, item) {
            context.commit('switTag', item)
        },
        handleClose(context, ind) {
            context.commit('handleClose', ind)
        },
        refresh(context, ind) {
            context.commit('refresh', ind)
        },
        colseRight(context, ind) {
            context.commit('colseRight', ind)
        },
        closeAll(context) {
            context.commit('closeAll')
        },
        option(context, route) {
            context.commit('option', route)
        },
        close(context) {
            context.commit('close')
        }
    }
}